package leetcode;

/**
 * @author ：徐梦轲
 * @date ：2022/5/22 0022 22:10
 */
class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
  }
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) {
            return null;
        }
        ListNode longNode = headA;
        ListNode shortNode = headB;
        int lenA = 0;
        int lenB = 0;
        int len = 0;
        while(longNode != null) {
            lenA++;
            longNode = longNode.next;
        }
        while(shortNode != null) {
            lenB++;
            shortNode = shortNode.next;
        }
        longNode = headA;
        shortNode = headB;

        if (lenA < lenB) {
            len = lenB - lenA;
            longNode = headB;
            shortNode = headA;
        }else {
            len = lenA - lenB;
        }
        while(len > 0) {
            longNode = longNode.next;
            len--;
        }
        while (longNode != shortNode) {

            longNode = longNode.next;
            shortNode = shortNode.next;
        }
        return longNode;
    }
}
